<template>
  <div class="cate">
    <el-bread></el-bread>

    <el-button @click="showAdd" class="el-btn" type="primary" size="small" plain
      >添加</el-button
    >

    <el-table
      :data="cateList"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="分类编号" width="180">
      </el-table-column>
      <el-table-column prop="catename" label="分类名称" width="180">
      </el-table-column>
      <el-table-column label="图片">
        <template v-slot="a">
          <el-image
            v-if="a.row.pid != 0 && a.row.img"
            fit="fill"
            :src="a.row.img"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template v-slot="a">
          <el-tag v-if="a.row.status == 1" type="success">启用</el-tag>
          <el-tag v-else type="warning">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="a">
          <el-button size="small" type="primary" @click="infoShow(a.row.id)"
            >编辑</el-button
          >
          <el-button size="small" type="danger" @click="del(a.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <v-dialog ref="dialog" />
  </div>
</template>

<script>
import { catedelete } from "../../request/api";
import vDialog from "./cate-dialog.vue";
import { mapState, mapActions } from "vuex";
export default {
  components: {
    vDialog,
  },
  mounted() {
    this.getCateList();
  },
  computed: {
    ...mapState("cate", ["cateList"]),
  },
  methods: {
    ...mapActions("cate", ["getCateList"]),
    showAdd() {
      this.$refs.dialog.isshow = true;
    },
    infoShow(id) {
      this.$refs.dialog.getInfo(id);
    },
    del(id) {
      this.$del(() => {
        catedelete({ id }).then((res) => {
          if (res.code == 200) {
            this.$message.success(res.msg);
            this.getCateList();
            return;
          }
          this.$message.error(res.msg);
        });
      });
    },
  },
};
</script>

<style scoped>
.el-btn {
  display: block;
  margin: 20px 0 5px 0;
}
</style>